package com.jc.service.exampleapp.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jc.service.common.api.CommonController;
import com.jc.service.common.model.PageResponse;
import com.jc.service.common.util.ResponseResult;
import com.jc.service.exampleapp.entity.ExampleApp;
import com.jc.service.exampleapp.model.ExampleAppQueryParam;
import com.jc.service.exampleapp.service.ExampleAppService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 示例应用管理控制器
 * 提供示例应用的增删改查接口
 */
@RestController
@RequestMapping("/api/v1/tenants/{tenantId}/apps")
@Tag(name = "示例应用管理", description = "示例应用的增删改查接口")
public class ExampleAppController extends CommonController {

    /**
     * 示例应用服务接口
     */
    private final ExampleAppService exampleAppService;

    /**
     * 日志记录器
     */
    private final Logger logger = org.slf4j.LoggerFactory.getLogger(ExampleAppController.class);

    /**
     * 构造函数注入示例应用服务
     *
     * @param exampleAppService 示例应用服务实现类
     */
    public ExampleAppController(ExampleAppService exampleAppService) {
        this.exampleAppService = exampleAppService;
    }

    /**
     * 创建示例应用
     *
     * @param tenantId   租户ID
     * @param exampleApp 示例应用对象
     * @return 创建成功的示例应用信息
     */
    @PostMapping
    @Operation(summary = "创建示例应用", description = "创建一个新的示例应用")
    public ResponseResult<ExampleApp> create(@Parameter(description = "租户ID") @PathVariable String tenantId, @Valid @RequestBody ExampleApp exampleApp) {
        logger.debug("准备创建示例应用: 租户:{},操作用户:{}", tenantId, getLoginUser().getLoginName());
        exampleApp.setTenantId(tenantId);
        return ResponseResult.success("创建成功", exampleAppService.create(exampleApp));
    }

    /**
     * 更新示例应用
     *
     * @param tenantId   租户ID
     * @param id         示例应用ID
     * @param exampleApp 更新的示例应用对象
     * @return 更新后的示例应用信息
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新示例应用", description = "根据ID更新示例应用信息")
    public ResponseResult<ExampleApp> update(
            @Parameter(description = "租户ID") @PathVariable String tenantId,
            @Parameter(description = "示例应用ID") @PathVariable String id,
            @Valid @RequestBody ExampleApp exampleApp) {
        logger.debug("更新示例应用: 租户:{},应用 ID：{},操作用户:{}", tenantId, id, getLoginUser().getLoginName());
        exampleApp.setId(id);
        exampleApp.setTenantId(tenantId);
        return ResponseResult.success("更新成功", exampleAppService.update(exampleApp));
    }

    /**
     * 根据ID获取示例应用详情
     *
     * @param tenantId 租户ID
     * @param id       示例应用ID
     * @return 示例应用详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取示例应用", description = "根据ID获取示例应用详情")
    public ResponseResult<ExampleApp> getById(
            @Parameter(description = "租户ID") @PathVariable String tenantId,
            @Parameter(description = "示例应用ID") @PathVariable String id) {
        logger.debug("获取示例应用: 租户:{},应用 ID：{},操作用户:{}", tenantId, id, getLoginUser().getLoginName());
        return ResponseResult.success(exampleAppService.getById(id));
    }

    /**
     * 获取示例应用列表
     * 支持分页和条件查询
     *
     * @param tenantId   租户ID
     * @param queryParam 查询参数对象
     * @return 示例应用分页列表
     */
    @GetMapping
    @Operation(summary = "获取示例应用列表", description = "获取示例应用列表，支持分页和条件查询")
    public ResponseResult<PageResponse<ExampleApp>> list(
            @Parameter(description = "租户ID") @PathVariable String tenantId,
            @Valid ExampleAppQueryParam queryParam) {

        // 设置租户ID
        queryParam.setTenantId(tenantId);
        Page<ExampleApp> page = new Page<>(queryParam.getPage(), queryParam.getPageSize());
        IPage<ExampleApp> result = exampleAppService.getWithPageAndCondition(page, queryParam);
        return ResponseResult.success(PageResponse.from(result));
    }

    /**
     * 根据ID删除示例应用
     *
     * @param tenantId 租户ID
     * @param id       示例应用ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除示例应用", description = "根据ID删除示例应用")
    public ResponseResult<Boolean> deleteById(
            @Parameter(description = "租户ID") @PathVariable String tenantId,
            @Parameter(description = "示例应用ID") @PathVariable String id) {
        logger.debug("删除示例应用: 租户:{},应用 ID：{},操作用户:{}", tenantId, id, getLoginUser().getLoginName());
        return ResponseResult.success("删除成功", exampleAppService.deleteById(id));
    }
}